package com.work.intelligent_transportation2;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import Api.LoginBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Sign_inActivity extends AppCompatActivity {
    private String url = "http://121.9.253.237:8888/traffic/api/v2/user_login";
    private OkHttpClient okHttp = new OkHttpClient();
    private LinearLayout linearLayout;
    private TextView textView;
    private TextView textView2;
    private EditText eduser;
    private EditText edpassword;
    private LinearLayout linearLayout2;
    private Button signInSignIn;
    private Button signInRegister;
    private CheckBox signInPassword;
    private CheckBox signInLogin;
    SharedPreferences SP;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SP=getSharedPreferences("IT",MODE_PRIVATE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        initView();
        signInSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = eduser.getText().toString().trim();
                String userPwd = edpassword.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("UserName", userName);
                        jsonObject.addProperty("UserPwd", userPwd);
                        String json = jsonObject.toString();
//MediaType对象包含了三种信息：type 、subtype以及charset，一般将这些信息传入parse()方法中，这样就可以解析出MediaType对象，比如 “text/x-markdown; charset=utf-8”
                        MediaType Medipet=MediaType.Companion.parse("application/json;charset=utf-8");
                        RequestBody requestBody=RequestBody.create(json,Medipet);
                        Request request=new Request.Builder().url(url).post(requestBody).build();
                        Call call=okHttp.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                Gson gson=new Gson();
                                LoginBean loginBean=gson.fromJson(result,LoginBean.class);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (loginBean.getRESULT().equals("S")){
                                            SharedPreferences.Editor editor= SP.edit();
                                            editor.putBoolean("记住密码",true);
                                            editor.putBoolean("自动登录",true);
                                            editor.putString("用户",userName);
                                            editor.putString("密码",userPwd);
                                            editor.apply();
                                            Intent intent = new Intent(Sign_inActivity.this, HomeActivity.class);
                                            startActivity(intent);
                                        }else if (loginBean.getRESULT().equals("F")){
                                            new AlertDialog.Builder(Sign_inActivity.this)
                                                    .setMessage("登录失败")
                                                    .create()
                                                    .show();
                                        }
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });
        signInRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Sign_inActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    private void initView() {
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
        eduser = (EditText) findViewById(R.id.eduser);
        edpassword = (EditText) findViewById(R.id.edpassword);
        linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
        signInSignIn = (Button) findViewById(R.id.sign_in_sign_in);
        signInRegister = (Button) findViewById(R.id.sign_in_register);
        signInPassword = (CheckBox) findViewById(R.id.sign_in_password);
        signInLogin = (CheckBox) findViewById(R.id.sign_in_login);
    }
    @Override
    protected void onStart() {
        boolean signinpassword=SP.getBoolean("记住密码",false);
        boolean singinlogin=SP.getBoolean("自动登录",false);
        super.onStart();
        String userName = SP.getString("用户","");
        String userPwd = SP.getString("密码","");
        if (signinpassword){
            signInPassword.setChecked(true);
            eduser.setText(userName);
            edpassword.setText(userPwd);
        }else {
            signInPassword.setChecked(false);
        }
        if (singinlogin){
            signInLogin.setChecked(true);
                    new AlertDialog.Builder(Sign_inActivity.this).setMessage("检测到账号或密码正确是否登录")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Sign_inActivity.this, HomeActivity.class);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
        }
        else {
            signInLogin.setChecked(false);
        }
    }
}